﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;

namespace ControlExplorer_Web
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://www.componentone.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService 
        : System.Web.Services.WebService
    {
        /// <summary>
        /// WebService used to upload files to the server, when the the can be broken into small parts.
        /// </summary>
        /// <param name="fileName">Name of the file</param>
        /// <param name="fileData">File content in Base64</param>
        /// <param name="parameters">Custom parameters</param>
        /// <param name="partCount">Total quantity of parts that will be uploaded</param>
        /// <param name="partNumber">Number of the part being uploaded</param>
        /// <returns>The url of the uploaded part in the server or an error message</returns>
        [WebMethod]
        public string UploadFile(string fileName, string fileData, List<Parameter> parameters, int partCount, int partNumber)
        {
            // get the file data and the file name to use in the server
            byte[] buffer = Convert.FromBase64String(fileData);
            string newFileName = C1UploaderHelper.GetServerPath(Server, fileName);

            if (C1UploaderHelper.ProcessPart(Context, new MemoryStream(buffer), newFileName, partCount, partNumber))
            {
                return C1UploaderHelper.GetUploadedFileUrl(Context, "WebService.asmx", fileName);
            }
            else
            {
                throw new Exception(C1UploaderHelper.ERROR_MESSAGE);
            }
        }


        /// <summary>
        /// WebService used to upload one or multiple files to the server.
        /// </summary>
        /// <param name="files"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        [WebMethod]
        public string UploadFiles(List<UploadFileInfo> files, List<Parameter> parameters)
        {
            string result = string.Empty;
            for (int i = 0; i < files.Count; i++)
            {
                var file = files[i];
                string url = UploadFile(file.FileName, file.FileData, parameters, 1, 1);

                // add the url of the uploaded file to the response
                result += (i == 0 ? string.Empty : "|") + url;
            }
            return result;
        }
    }

    /// <summary>
    /// Parameter class
    /// </summary>
    public class Parameter
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }

    /// <summary>
    /// Uploaded file
    /// </summary>
    public class UploadFileInfo
    {
        public string FileName { get; set; }
        public string FileData { get; set; }
    }
}
